/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.happy.admin.config;

import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.happy.admin.utils.DateSourceUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * mybatis-plus配置
 *
 * @author Mark sunlightcs@gmail.com
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(DataSourceProperties.class)
@MapperScan(basePackages = MybatisPlusConfig.PACKAGE,sqlSessionFactoryRef="sqlSessionFactory")
public class MybatisPlusConfig {

    public static final String PACKAGE = "com.happy.film.mapper";
    private static final String MAPPER_LOCATION = "classpath*:/mapper/**/*.xml";
    static final String CONFIG_LOCATION = "classpath:mybatis-config.xml";

    @Autowired
    private DataSourceProperties dataSourceProperties;


    @Bean
    @Primary
    public DataSource dataSource(){
        log.info("数据库的配置为 {},用户名 为{}",dataSourceProperties.getUrl(),dataSourceProperties.getUserName());
        return DateSourceUtils.create(dataSourceProperties.getUrl(),dataSourceProperties.getUserName(),dataSourceProperties.getPassword(),dataSourceProperties.getDriver());
    }


    @Bean(value = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
//        mybatisSqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(CONFIG_LOCATION));
        Resource[] resource = new PathMatchingResourcePatternResolver()
                .getResources(MybatisPlusConfig.MAPPER_LOCATION);
        mybatisSqlSessionFactoryBean.setMapperLocations(resource);
        mybatisSqlSessionFactoryBean.setPlugins(new Interceptor[]{new MybatisInterceptor(),new PaginationInterceptor()});
        mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig());
        return mybatisSqlSessionFactoryBean.getObject();
    }

    @Bean
    public GlobalConfig globalConfig(){
        GlobalConfig globalConfig = new GlobalConfig();
//        globalConfig.setMetaObjectHandler(metaObjectHandler);
        return globalConfig;
    }

    /**
     * 分页插件
//     */
//    @Bean
//    public PaginationInterceptor paginationInterceptor() {
//        return new PaginationInterceptor();
//    }

}
